Practice 1

  1. Download the coastline shapefile on your own computer.
  2. Read the shapefile with the st_read() function.

If you want, you can try with your own data or download other shapefiles from https://www.naturalearthdata.com/

Solution

# if sf is not installed
# install.packages("sf")
library(sf)

# download the data 
download.file("http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/physical/ne_10m_coastline.zip", 
              destfile = 'assets/data/coastlines.zip')

# unzip the file
unzip(zipfile = "assets/data/coastlines.zip", 
      exdir = 'assets/data/ne-coastlines-10m')

# WARNING: You have to define you own path in destfile argument and exdir.

# Read the file
coast <- read_sf("assets/data/ne-coastlines-10m")

Make a visual check of the spatial R object

# if mapview is not installed
# install.packages("mapview")
library(mapview)
mapview(coast)

Manage your active directory

To import files on R from your computer, you have to set properly active working directory: where R is reading and writing files on your computer. Some hints / functions to deal with that aspect.

getwd() # Print the path of the active folder / working directory
setwd(choose.dir()) 
dir() # function which list all the files in the working directory.
  1. setwd() is a function to set a new active directory
  2. choose.dir() works exclusively on windows, This function opens a dialog box to set your custom working directory